<?php

namespace App\Admin\Eloquent;

use DB;

class OrganizationalUser extends Model
{
    protected $primaryKey = 'user_id';

    protected $fillable = [
        'status',
        'user_id',
        'company_name',
        'address',
        'license_no',
        'license_address',
        'license_expire_date',
        'business_scope',
        'organization_no',
        'summary',
        'legal_person',
        'cert_no',
        'legal_person_phone',
        'bank_code',
        'bank_account_no',
        'province',
        'city',
        'bank_branch',
        'yyzz',
        'zzjgz',
        'swdjz',
        'jsxkz',
        'frzjz',
        'frzjf',
        'audit_order_no',
        'status_info',
        'telephone',
        'email',
        'code',
        'profit_ratio'
    ];

    /**
     * 获取关联的用户
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\Admin\Eloquent\User');
    }

    /**
     * 查询当前机构下的所有部门
     * 
     * @return [type] [description]
     */
    public function deptList(int $pagesize = 15){
        return Code::where('type_id', '=', 'DEPARTMENT')->where('parent_code', '=', $this->code)->paginate($pagesize);
    }

    /**
     * 查询指定机构下的所有部门
     * 
     * @param  string      $org_code [description]
     * @param  int|integer $pagesize [description]
     * @return [type]                [description]
     */
    public static function depts(string $org_code,int $pagesize = 15){

        return Code::where('type_id', '=', 'DEPARTMENT')->where('parent_code', '=', $org_code)->paginate($pagesize);
    }

    /**
     * 查询当前机构下的所有经纪人
     * 
     * @param  int|integer $pagesize [description]
     * @return [type]                [description]
     */
    public function brokerList(array $args, int $pagesize = 15){

        $query = DB::table('user_info as a')
            ->leftJoin('codes as b', 'a.dept_code', '=', 'b.code')
            ->leftJoin('users as c', 'a.user_id', '=' ,'c.id')
            ->selectRaw('a.user_id,a.name,a.invite_code,b.name as dept,c.phone_number,a.created_at ')
            ->whereRaw('a.invite_code = a.broker_code')
            ->where('a.org_code', $this->code);


        if(!empty($args['name']))
            $query = $query->where('a.name', 'like', $args['name']);

        if(!empty($args['dept_no']))
            $query = $query->where('a.dept_code', $args['dept_no']);

        $brokers = $query->orderBy('c.created_at', 'desc')->paginate(15);

        return $brokers;
    }

    /**
     * 查询该机构指定部门的人员
     * 
     * @param  string      $deptCode [description]
     * @param  array       $query    [description]
     * @param  int|integer $pagesize [description]
     * @return [type]                [description]
     */
    public function deptUserList(string $deptCode, array $args = [], int $pagesize = 15){
        $query = UserInfo::leftJoin('users as b', 'user_info.user_id', '=', 'b.id');

                if(isset($args['role_id']) && !empty($args['role_id']))                                     //角色id
                    $query = $query->join('role_user as c', 'user_info.user_id', '=', 'c.user_id');

                $query = $query->where('user_info.org_code', '=', $this->code);
                $query = $query->where('user_info.dept_code', '=', $deptCode);

        if(isset($args['username']) && !empty($args['username']))                                   //用户名
            $query = $query->where('b.username', 'like', '%'.$args['username'].'%');

        if(isset($args['name']) && !empty($args['name']))                                           //姓名
            $query = $query->where('user_info.name', 'like', '%'.$args['name'].'%');

        if(isset($args['role_id']) && !empty($args['role_id']))                                     //角色id
            $query = $query->where('c.role_id', '=', $args['role_id']);

        if(isset($args['broker_code']) && !empty($args['broker_code']))                              //经纪人code
            $query = $query->where('user_info.broker_code', '=', $args['broker_code'])->where('user_info.invite_code', '<>', $args['broker_code']);

        return $query->paginate($pagesize);
    }
}